package org.vt.eldercare.ui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import org.vt.eldercare.data.Node;

public class RightClickOnNodeListener extends MouseAdapter {

	private JPopupMenu menu;

	public RightClickOnNodeListener(final Node n, final Frame parent) {
		super();
		menu = new JPopupMenu();
		final JMenuItem item = menu.add("Rename...");
		final JMenuItem item2 = menu.add("Edit Node Configuration...");
		
		item2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				NodeConfigurationDialog dialog = new NodeConfigurationDialog(
						parent, n);
				dialog.setLocationRelativeTo(parent);
				dialog.setVisible(true);
			}
		});
		item.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String s = JOptionPane.showInputDialog(item, null, "Rename Node", JOptionPane.PLAIN_MESSAGE);
				if(s != null && !s.isEmpty()){
					n.setName(s);
				}
			}

		});
	}

	@Override
	public void mouseClicked(MouseEvent evt) {
		super.mouseClicked(evt);
		if (evt.isPopupTrigger()) {
			menu.show(evt.getComponent(), evt.getX(), evt.getY());
		}
	}

	@Override
	public void mousePressed(MouseEvent evt) {
		super.mouseClicked(evt);
		if (evt.isPopupTrigger()) {
			menu.show(evt.getComponent(), evt.getX(), evt.getY());
		}
	}

	@Override
	public void mouseReleased(MouseEvent evt) {
		super.mouseClicked(evt);
		if (evt.isPopupTrigger()) {
			menu.show(evt.getComponent(), evt.getX(), evt.getY());
		}
	}

}
